package org.jeecg.modules.demo3.fly.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo3.fly.entity.AxhkLandManagement;
import org.jeecg.modules.demo3.fly.entity.AxhkWorkManagement;
import org.jeecg.modules.demo3.fly.entity.assist.AxhkLandManagements;
import org.jeecg.modules.demo3.fly.entity.assist.WorkFinish;
import org.jeecg.modules.demo3.fly.entity.assist.qun.AxhkWorkManagements;
import org.jeecg.modules.demo3.fly.service.IAxhkWorkManagementService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo3.fly.service.IAxhkYwAgrplaneService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 作业管理
 * @Author: jeecg-boot
 * @Date:   2021-01-28
 * @Version: V1.0
 */
@Api(tags="作业管理")
@RestController
@RequestMapping("/fly/axhkWorkManagement")
@Slf4j
public class AxhkWorkManagementController extends JeecgController<AxhkWorkManagement, IAxhkWorkManagementService> {
	@Autowired
	private IAxhkWorkManagementService axhkWorkManagementService;
	@Autowired
	private IAxhkYwAgrplaneService axhkYwAgrplaneService;
	/**
	 * 分页列表查询
	 *
	 * @param axhkWorkManagement
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "作业管理-分页列表查询")
	@ApiOperation(value="作业管理-分页列表查询", notes="作业管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AxhkWorkManagement axhkWorkManagement,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<AxhkWorkManagement> queryWrapper = QueryGenerator.initQueryWrapper(axhkWorkManagement, req.getParameterMap());
		 Page<AxhkWorkManagement> page = new Page<AxhkWorkManagement>(pageNo, pageSize);
		 IPage<AxhkWorkManagement> pageList = axhkWorkManagementService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	/**
	 *   添加
	 *
	 * @param axhkWorkManagement
	 * @return
	 */
	@AutoLog(value = "作业管理-添加")
	@ApiOperation(value="作业管理-添加", notes="作业管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AxhkWorkManagement axhkWorkManagement) {
		axhkWorkManagementService.save(axhkWorkManagement);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param axhkWorkManagement
	 * @return
	 */
	@AutoLog(value = "作业管理-编辑")
	@ApiOperation(value="作业管理-编辑", notes="作业管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AxhkWorkManagement axhkWorkManagement) {
		axhkWorkManagementService.updateById(axhkWorkManagement);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "作业管理-通过id删除")
	@ApiOperation(value="作业管理-通过id删除", notes="作业管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		System.out.println("进入删除界面，开始你的操作！");
		axhkWorkManagementService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "作业管理-批量删除")
	@ApiOperation(value="作业管理-批量删除", notes="作业管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.axhkWorkManagementService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "作业管理-通过id查询")
	@ApiOperation(value="作业管理-通过id查询", notes="作业管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AxhkWorkManagement axhkWorkManagement = axhkWorkManagementService.getById(id);
		if(axhkWorkManagement==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(axhkWorkManagement);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param axhkWorkManagement
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AxhkWorkManagement axhkWorkManagement) {
        return super.exportXls(request, axhkWorkManagement, AxhkWorkManagement.class, "作业管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AxhkWorkManagement.class);
    }

	 //通过无人机序列号查询无人机是否存在-wjn
    @GetMapping("/serialNumber")
	 public Result<?> getserialNumber(String workId,int serialNumber){
		axhkYwAgrplaneService.getByserialNumber(workId,serialNumber);
		return Result.OKT("添加成功");
	}

	 //通过作业Id查询，作业下的农机-——wjn
	 @GetMapping("/workId")
	 public Result<?> getWorkId(String id){
    	List<Map<String,Object>> YwAgrplaneList = axhkYwAgrplaneService.getByWorkId(id);
    	return Result.OK(YwAgrplaneList);
	 }

	 /**
	  * wjn
	  * filePath  文件路径
	  * fileName  文件名称（需要带后缀，如*.jpg、*.java、*.xml）
	  */
	 //添加地块管理-wjn
	 @PostMapping("/addLands")
	 public Result<?> getInsertLands(@RequestBody AxhkLandManagement axhkLandManagement) {
		 System.out.println("有人访问！！！");
		 String  id = axhkWorkManagementService.addLands(axhkLandManagement);
		 if(id == null){
			 return Result.OKF("添加不成功，地块名称:不能完全由数字组成或已存在此地块名称!");
		 }else{
			 return Result.OK("上传成功！",id);
		 }
	 }

	 //读取文件获得三坐标-wjn
	 @GetMapping("getLatitude")
	 public Result<?> getThreeLatitude(String massifname) throws Exception{
		 ArrayList<ArrayList> lists = axhkWorkManagementService.getByPathName(massifname);
		 if(lists.size() !=0 ){
             return Result.OK("获取成功！",lists);
         }else{
             return Result.OKF("获取不成功！");
         }
	 }


	 //根据ID删除-wjn
	 @DeleteMapping("getByPlaneId")
	 public Result<?> getByPlanesId(Integer id){
	 	int i = axhkYwAgrplaneService.getPlaneId(id);
	 	if(i>0){
	 		return Result.OKT("删除成功");
		}else{
	 		return Result.OKF("删除失败！");
		}
	 }

	 //通过群id查询该群下 所有地块信息返还给APP
	 @GetMapping("getCoordinate")
	 public Result<?> getByWorkId(String fluckId){
	 	ArrayList<AxhkLandManagement> axhkLandManagement = axhkWorkManagementService.getByWorkId(fluckId);
	 	return Result.OK("返回成功!",axhkLandManagement);
	 }

	 //通过手机APP添加任务 -wjn
	 @PostMapping("getWorkManagement")
	 public Result<?> getWorkManagement(@RequestBody AxhkWorkManagement axhkWorkManagement){
		 int i = axhkWorkManagementService.addWork(axhkWorkManagement);
	 	if(i>0){
	 		return Result.OKT("添加成功!");
		}else {
	 		return Result.OKF("添加失败!");
		}
	 }

	 //根据群id获取到 该群所有任务信息-地块信息  返回给手机APP
	 @GetMapping("getWorkAndLand")
	 public Result<?> getWorkAndLand(String fluckId){
		 ArrayList<List> lists = axhkWorkManagementService.getByWorkAndLand(fluckId);
		 return Result.OK("操作成功!",lists);
	 }

	 //手机app修改 地块信息
	@PostMapping("/getNewLand")
	 public Result<?> getNewLand(@RequestBody AxhkLandManagement axhkLandManagement){
	 	String id = axhkWorkManagementService.getByUpdateLandMang(axhkLandManagement);
	 	if(null != id){
	 		return Result.OK("更新成功！",id);
		}else {
	 		return Result.OKF("更新失败！");
		}
	}

	 //根据地块 id 删除地块信息 以及 本地地块坐标文件
	 @GetMapping("getDeleteLand")
	public Result<?> getDeleteLand(String id){
	 	int i = axhkWorkManagementService.getByDeleteLandMang(id);
	 	if(i == -1){
			return Result.OKF("该地块存在于任务里，不能够删除！");
		}
	 	if(i>0){
			return Result.OKT("删除成功!");
		}else{
			return Result.OKF("删除失败!");
		}
	 }

	 /*
	  * 通过客户端传递的经纬度 计算现有 距离最近地块的位置
	  * telephone 电话号码
	  * endLat 位置 经度
	  * endLng 位置 维度
	 * */
	 @GetMapping("getScope")
	 public Result<?> getScope(String fluckId, double endLat, double endLng){
		 ArrayList<AxhkLandManagement> axhkLandManagement = axhkWorkManagementService.getByScope(fluckId,endLat,endLng);
		 if(null != axhkLandManagement) {
			 return Result.OK("获取成功!", axhkLandManagement);
		 }else{
			 return Result.OKF("附近没有小与5公里的地块!");
		 }
	 }

	 /*
	  *修改 任务完成的 标识
	  * String workId 任务id
	  * String createBy 用户名 修改人
	  * */
	 @GetMapping("getByUpdateFinish")
	 public Result<?> getByUpdateFinish(String createBy,String workId){
	 	String masing = axhkWorkManagementService.getUpdateFinih(createBy,workId);
	 	switch (masing){
			case "A":
				return Result.OKF("未修改成功！");
			case "B":
				return Result.OKF("任务ID不能为空！");
			default:
				return Result.OKT("修改成功！");
		}
	 }

	 /*
	  * 查看该用户下所有任务的完成信息
	  * 查看该群下所有任务的完成信息
	  * String createBy
	  * String fluckId
	 * */
	@GetMapping("getByAllFinishWork")
	 public Result<?> getByAllFinishWork(String createBy,String fluckId){
		WorkFinish workFinish = axhkWorkManagementService.AllFinishWork(createBy,fluckId);
		if(null != workFinish){
			return Result.OK(workFinish);
		}else{
			return Result.OKF("未获取到！");
		}
	}
}
